<?php
/**
 * 评论管理
 */

class Content_CommentController extends Cms_AdminController {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init ();
	}
	
	public function indexAction() {
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$DB_Comment = new DB_Comment ();
		$where = "";
		
		$Page = new Cms_Pager ( $DB_Comment );
		
		$Pagelimit = $Page->loadPagerData ( 6, $where );
		
		$Comment_list = $DB_Comment->loadList ( '*', $where, "id DESC", $Pagelimit );
		if ($Comment_list) {
			$Model_News = new Model_News ();
			foreach ( $Comment_list as $key => $val ) {
				$Comment_list [$key] ['news'] = $Model_News->Get_News ( $val ['newid'] );
			}
		}
		
		$this->view->assign ( "pageBlock", $Page->loadPagerTpl () );
		$this->view->assign ( 'Comment_list', $Comment_list );
		$this->render ( 'Content/comment/index', null, true );
	}
	
	/**
	 * 显示评论内容
	 */
	public function showAction() {
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$commentid = $this->_request->get ( 'commentid' );
		
		$DB_Comment = new DB_Comment ();
		$where = "newid={$commentid}";
		
		$Page = new Cms_Pager ( $DB_Comment );
		
		$Pagelimit = $Page->loadPagerData ( 5, $where );
		
		$Comment_list = $DB_Comment->loadList ( '*', $where, "id DESC", $Pagelimit );
		if ($Comment_list) {
			$this->view->assign ( 'commentid', $commentid );
			$this->view->assign ( "pageBlock", $Page->loadPagerTpl () );
			$this->view->assign ( 'Comment_list', $Comment_list );
			$this->render ( 'Content/comment/show', null, true );
		} else {
			$this->view->perRedirect ( '暂无评论！', 'blank', '', '', '' );
		}
	}
	
	/**
	 * 删除评论
	 */
	public function delAction() {
		$id = $this->_request->getPost ( 'id' );
		if (isset ( $id ) && ! empty ( $id )) {
			$Model_Comment = new Model_Comment ();
			if ($Model_Comment->delete_Comment ( $id )) {
				exit ( '1' );
			} else {
				exit ( '0' );
			}
		} else {
			exit ( '-1' );
		}
	}
	
	/**
	 * 批量删除
	 */
	public function batchdelAction() {
		$ids = $this->_request->getPost ( 'ids' );
		$commentid = $this->_request->getPost ( 'commentid' );
		if (! is_array ( $ids ) || count ( $ids ) <= 0) {
			$this->view->perRedirect ( '请至少选择一个！', "/content/comment/show/commentid/{$commentid}/" );
		}
		$Model_Comment = new Model_Comment ();
		foreach ( $ids as $id ) {
			$Model_Comment->delete_Comment ( $id );
		}
		$this->view->perRedirect ( '删除评论成功!', "/content/comment/show/commentid/{$commentid}/" );
	}
	
	/**
	 * 评论搜索
	 */
	public function searchAction() {
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$search = $this->_request->getPost ( 'search' );
		$searchtype = $this->_request->getPost ( 'searchtype' );
		$keyword = $this->_request->getPost ( 'keyword' );
		
		$DB_Comment = new DB_Comment ();
		$where = " 1=1 ";
		if ($searchtype == 0) {
			$DB_News = new DB_News ();
			$News_list = $DB_News->loadList ( "id,catid,title", "title like '%$keyword%'" );
			
			if ($News_list) {
				$newid_array = array ();
				foreach ( $News_list as $val ) {
					array_push ( $newid_array, $val ['id'] );
				}
				$newid = join ( ',', $newid_array );
				$where .= " and newid in({$newid})";
			}
		} elseif ($searchtype == 1) {
			$where .= " and newid={$keyword}";
		} elseif ($searchtype == 2) {
			$where .= " and username='{$keyword}'";
		}
		
		$Page = new Cms_Pager ( $DB_Comment );
		
		$Pagelimit = $Page->loadPagerData ( 6, $where );
		
		$Comment_list = $DB_Comment->loadList ( '*', $where, "id DESC", $Pagelimit );
		if ($Comment_list) {
			$Model_News = new Model_News ();
			foreach ( $Comment_list as $key => $val ) {
				$Comment_list [$key] ['news'] = $Model_News->Get_News ( $val ['newid'] );
			}
		}
		
		$this->view->assign ( "pageBlock", $Page->loadPagerTpl () );
		$this->view->assign ( 'Comment_list', $Comment_list );
		$this->render ( 'Content/comment/index', null, true );
	}
	
	/**
	 * 批量移动
	 */
	public function batchremoveAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		$ids = $this->_request->getPost ( 'ids' );
		$catid = $this->_request->getPost ( 'catid' );
		
		if (isset ( $dosubmit )) {
			if (empty ( $ids )) {
				$this->view->perRedirect ( '参数错误,请正确选择要移动的新闻！', "/content/manage/remove/id/{$ids}/" );
			}
			if (empty ( $catid )) {
				$this->view->perRedirect ( '请选择目标栏目！', "/content/manage/remove/id/{$ids}/" );
			}
			
			$DB_News = new DB_News ();
			$data = array (
					"catid" => $catid 
			);
			if ($DB_News->updateByConditions ( $data, "id in({$ids})" )) {
				$this->view->perRedirect ( '移动内容成功！', 'blank', '', 'remove' );
			} else {
				$this->view->perRedirect ( '系统错误，移动内容失败！', "/content/manage/remove/id/{$ids}/" );
			}
		} else {
			$this->view->perRedirect ( '参数错误！', "/content/manage/remove/id/{$ids}/" );
		}
	}
}
?>